﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace DonationTracker
{
    public partial class Donations : System.Web.UI.Page
    {
        private string uid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Administrator"))
                {
                    GridView1.AutoGenerateEditButton = true;
                    GridView1.AutoGenerateDeleteButton = true;
                }
                else if (User.IsInRole("Volunteer"))
                {
                    GridView1.AutoGenerateEditButton = true;
                }
                if(!IsPostBack) GridView1.ShowFooter = false;
                uid = Membership.GetUser().ProviderUserKey.ToString();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert" && Page.IsValid)
            {
                //Locate the controls in the Gridview's footer and access them.
                DropDownList ddlDonor = (DropDownList)GridView1.FooterRow.FindControl("ddlDonor");
                TextBox txtDesc = (TextBox)GridView1.FooterRow.FindControl("txtDesc");
                DropDownList ddlCharity = (DropDownList)GridView1.FooterRow.FindControl("ddlCharity");
                
                //Trim any extra spaces from the beginning/end of description text.
                string desc = txtDesc.Text.Trim();
                //If there isn't any description set desc = null.
                if (String.IsNullOrWhiteSpace(desc)) desc = null;

                //Set the values of the insert parameteres
                donationTracker_dataSource.InsertParameters["donorID"] = 
                    new Parameter("donorID", System.Data.DbType.Int32, ddlDonor.SelectedValue);
                donationTracker_dataSource.InsertParameters["description"] =
                    new Parameter("description", System.Data.DbType.String, desc);
                donationTracker_dataSource.InsertParameters["charityID"] =
                    new Parameter("charityID", System.Data.DbType.Int32, ddlCharity.SelectedValue);
                donationTracker_dataSource.InsertParameters["enteredBy"] =
                    new Parameter("enteredBy", System.Data.DbType.Guid, uid);
                //Insert new record.
                donationTracker_dataSource.Insert();
                GridView1.ShowFooter = false;
            }
        }

        protected void btnAddDonation_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = true;
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                DropDownList ddlEditDonor =
                    (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlEditDonor");
                TextBox txtEditDesc =
                    (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditDesc");
                DropDownList ddlCharity =
                    (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlEditCharity");

                string editDesc = txtEditDesc.Text.Trim();
                if (String.IsNullOrWhiteSpace(editDesc))
                    editDesc = null;

                donationTracker_dataSource.UpdateParameters["donorID"] =
                    new Parameter("donorID", System.Data.DbType.Int32, ddlEditDonor.SelectedValue);
                donationTracker_dataSource.UpdateParameters["description"] =
                    new Parameter("description", System.Data.DbType.String, editDesc);
                donationTracker_dataSource.UpdateParameters["charityID"] =
                    new Parameter("charityID", System.Data.DbType.Int32, ddlCharity.SelectedValue);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GridView1.ShowFooter = false;
        }
    }
}